package main

import (
	"flag"
	"fmt"
	"log"
	"net"
	"os"

	"gitee.com/spark-community-works-collections/shenmo-water-pipe/utils"
)

var from int
var to int
var server string

func init() {
	flag.IntVar(&from, "from", 0, "")
	flag.IntVar(&to, "to", 0, "")
	flag.StringVar(&server, "server", "localhost", "")
	flag.Parse()
}

const (
	OP_SEND int = iota
	OP_RECV
)

func main() {
	var conn net.Conn
	var operat int
	var err error

	if flag.Lookup("from") != nil && from != 0 {
		conn, err = net.Dial("tcp", fmt.Sprintf("%v:%v", server, from))
		if err != nil {
			log.Fatal(err)
		}
		operat = OP_RECV
	} else if flag.Lookup("to") != nil && to != 0 {
		conn, err = net.Dial("tcp", fmt.Sprintf("%v:%v", server, to))
		if err != nil {
			log.Fatal(err)
		}
		operat = OP_SEND
	}

	if operat == OP_RECV {
		utils.TransferData(conn, os.Stdout)
	}

	if operat == OP_SEND {
		utils.TransferData(os.Stdin, conn)
	}
}
